package com.cennavi.bigdata.baotou.util;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zuoweiguang on 2016/11/9.
 */
public class MongoCreateIndex {

    JSONObject prop = PropertiesUtil.getProperties();
    MongoClient mongoClient = null;
    DB db = null;
    DBCollection correspondingCol = null;
    DBCollection trafficStatusCol = null;

    public MongoCreateIndex () {
        ServerAddress serverAddress = new ServerAddress(prop.getString("mongoHost"), prop.getInteger("mongoPort"));
        String host = prop.getString("mongoHost");
        if (host.equals("192.168.4.128")) {
            mongoClient = new MongoClient(serverAddress);
        } else {
            List mongoCredentialList = new ArrayList();
            mongoCredentialList.add(MongoCredential.createCredential("sa", "admin", "mm@2016".toCharArray()));
            mongoClient = new MongoClient(serverAddress, mongoCredentialList);
        }
        db = mongoClient.getDB(prop.getString("mongoDb"));
        correspondingCol = db.getCollection("corresponding");
        correspondingCol.drop();
        trafficStatusCol = db.getCollection("traffic_status");
        trafficStatusCol.drop();
    }

    public static void createIndex(DBCollection col, String fieldName) {
        col.createIndex(fieldName);
    }

    public static void main(String[] args) {
        MongoCreateIndex mci = new MongoCreateIndex();
    }

}
